/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.activity.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykht.proj.activity.entity.VDiscountPlanShop;
import com.ykht.proj.activity.entity.VGroupShop;
import com.ykht.proj.activity.vo.SjObject;
import com.ykht.proj.activity.vo.VDiscountPlanVo;
import com.ykht.proj.activity.vo.VGroupVo;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.log.annotation.SysLog;
import com.ykht.proj.activity.entity.VGroup;
import com.ykht.proj.activity.service.VGroupService;
import com.ykht.proj.coupon.api.feign.RemoteShopService;
import com.ykht.proj.coupon.entity.CnShop;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 群组表
 *
 * @author csg
 * @date 2021-05-18 14:26:57
 */
@RestController
@AllArgsConstructor
@RequestMapping("/vgroup" )
@Api(value = "vgroup", tags = "群组表管理")
@Slf4j
public class VGroupController {

    private final VGroupService vGroupService;

	private final RemoteShopService remoteShopService;

    /**
     * 门店群组分页查询
     * @param page 分页对象
     * @param vGroup 群组表
     * @return
     */
    @ApiOperation(value = "门店群组分页查询", notes = "门店群组分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('activity_vgroup_view')" )
    public R getVGroupPage(Page page, VGroup vGroup) {
		log.info("->门店群组分页查询{}", vGroup);
		IPage<VGroupVo> voIPage = vGroupService.getVGroupPage(page, vGroup);
        return R.ok(voIPage);
    }


    /**
     * 通过id查询群组表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/getById" )
    public R<VGroupVo> getById(Long id) {
		VGroup vGroup = vGroupService.getById(id);
		VGroupVo vGroupVo = BeanUtil.toBean(vGroup, VGroupVo.class);
		List<VGroupShop> groupShopList = new VGroupShop().selectList(
				Wrappers.lambdaQuery(VGroupShop.class).eq(VGroupShop::getGroupId, id));
		if (groupShopList != null && groupShopList.size() > 0){
			List<Long> collect = groupShopList.stream().map(VGroupShop::getShopId).collect(Collectors.toList());
			vGroupVo.setShopIdList(collect);
		}
		return R.ok(vGroupVo);
    }

    /**
     * 新增群组表
     * @param vGroupVo 群组表
     * @return R
     */
    @ApiOperation(value = "新增群组表", notes = "新增群组表")
    @SysLog("新增群组表" )
    @PostMapping
//    @PreAuthorize("@pms.hasPermission('activity_vgroup_add')" )
    public R save(@RequestBody VGroupVo vGroupVo) {
		log.info("->新增群组表{}", vGroupVo);
		if (null == vGroupVo.getSjObjectList() || vGroupVo.getSjObjectList().size() == 0){
			return R.failed("请选择参加群组的门店");
		}
		vGroupService.addVGroup(vGroupVo);
        return R.ok();
    }

	@ApiOperation(value = "校验 群组名称")
	@GetMapping("/checkGroupName")
	public R checkGroupName(VGroup vGroup) {
		log.info("->校验 群组名称{}", vGroup);
		VGroup group = new VGroup().selectOne(Wrappers.lambdaQuery(VGroup.class)
				.eq(VGroup::getGroupName, vGroup.getGroupName()));
		return R.ok(group == null ? true : group.getId().equals(vGroup.getId()) ? true : false);
	}

    /**
     * 修改群组表
     * @param vGroupVo 群组表
     * @return R
     */
    @ApiOperation(value = "修改群组表", notes = "修改群组表")
    @SysLog("修改群组表" )
    @PutMapping
//    @PreAuthorize("@pms.hasPermission('activity_vgroup_edit')" )
    public R updateById(@RequestBody VGroupVo vGroupVo) {
		log.info("->修改群组表{}", vGroupVo);
		if (null == vGroupVo.getSjObjectList() || vGroupVo.getSjObjectList().size() == 0){
			return R.failed("请选择参加群组的门店");
		}
		vGroupService.updateVGroup(vGroupVo);
        return R.ok();
    }

    /**
     * 通过id删除群组表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除群组表", notes = "通过id删除群组表")
    @SysLog("通过id删除群组表" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('activity_vgroup_del')" )
    public R removeById(@PathVariable Long id) {
        return vGroupService.deleteGroup(id);
    }

}
